from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field
from datetime import datetime, timezone
from services.server_integration.schemas.language_string import LanguageString
from services.server_integration.schemas.unit_configuration import CalibrationState


class CalibrateMessageKInd(Enum):
    Ordinary = 'Ordinary'
    Blessed = 'Blessed'
    Infernal = 'Infernal'


class CalibrateMessageActionKind(Enum):
    Push = 'Push'
    Update = 'Update'


class CalibrateMessage(BaseModel):
    action_kind: CalibrateMessageActionKind
    kind: CalibrateMessageKInd = CalibrateMessageKInd.Ordinary
    msg: Optional[LanguageString | str] = None
    timestamp: int = Field(default_factory=lambda: datetime.now().timestamp())

    def with_timed_msg(self, tz: timezone, language: str):
        dt = datetime.fromtimestamp(self.timestamp, tz).strftime('%Y-%m-%d %H:%M:%S')
        return CalibrateMessage(
            kind=self.kind,
            msg=f'[{dt}] {getattr(self.msg, language)}',
            timestamp=self.timestamp,
            action_kind=self.action_kind,
        )


class CalibrateHistoryMessage(BaseModel):
    kind: CalibrateMessageKInd = CalibrateMessageKInd.Ordinary
    msg: Optional[LanguageString | str] = None
    timestamp: int = Field(default_factory=lambda: datetime.now().timestamp())

    def with_timed_msg(self, tz: timezone, language: str):
        dt = datetime.fromtimestamp(self.timestamp, tz).strftime('%Y-%m-%d %H:%M:%S')
        return CalibrateHistoryMessage(
            kind=self.kind,
            msg=f'[{dt}] {getattr(self.msg, language)}',
            timestamp=self.timestamp,
        )


class CalibrateHistory(BaseModel):
    messages: list[CalibrateHistoryMessage] = []
    date: Optional[datetime] = None


class CalibrateHistoryInfo(BaseModel):
    date: Optional[datetime] = None
    calibration_state: CalibrationState = CalibrationState.UNCALIBRATED,
    ustirovka_state: bool = False,
    simulation_state: bool = False
